package com.fenrirtec.aepp.manage.api;


import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.fenrirtec.aepp.common.dto.TenderProductDto;
import com.fenrirtec.aepp.common.service.TenderProductService;
import com.fenrirtec.aepp.common.service.impl.TenderProductServiceImpl;
import com.fenrirtec.aepp.common.support.JSONActionSupport;
import com.fenrirtec.core.definition.BaseDefinition.API;
import com.fenrirtec.core.exception.DatabaseException;
import com.fenrirtec.core.exception.UnexpectedException;
import com.fenrirtec.core.utils.JsonUtils;

public class TenderProductAPI extends JSONActionSupport{
	
	private static final long serialVersionUID = 1L;

	private static final Logger logger = LoggerFactory.getLogger(TenderProductAPI.class);

	public TenderProductAPI() {
	}


	private String param;

	public String getParam() {
		return this.param;
	}

	public void setParam(String param) {
		this.param = param;
	}

	@Override
	protected List<ErrorInfo> doValidate() {
		return null;
	}
	
	@Override
	protected JSONObject doExecute() {
		TenderProductService tenderProductservice = new TenderProductServiceImpl();
		JSONObject root = new JSONObject();
		List<TenderProductDto> tenderProductDtolist = new ArrayList<TenderProductDto>();
		try {
			JSONObject jsonObj = JSONObject.parseObject(this.param);
			Integer tenderid=jsonObj.getInteger("tenderId");
			//Integer tenderid = 1;
			tenderProductDtolist=tenderProductservice.findTenderProduct(tenderid);
	
		} catch (DatabaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error("database error occurred.", e);
			}
			throw new JSONActionStatusException(API.Common.Status.DB_ERROR, e);
		} catch (UnexpectedException e) {
			if (logger.isErrorEnabled()) {
				logger.error("unexpected error occurred.", e);
			}
			throw new JSONActionStatusException(API.Common.Status.DB_ERROR, e);
		} 
		
		if(tenderProductDtolist !=null && tenderProductDtolist.size()>0){
			root.put("total", tenderProductDtolist.size());
			root.put("tplist", JsonUtils.fromBean(tenderProductDtolist));
		}
		return root;
	}	
}
